package io.renren.modules.app.service.impl;


import io.renren.common.utils.R;
import io.renren.modules.app.dao.ReportMoney;
import io.renren.modules.app.service.AppMoneyWebService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("appMoneyWebService")
public class AppMoneyWebServiceImpl  implements AppMoneyWebService {

    @Autowired
    private ReportMoney reportMoney;
    @Override
    public R moneyReport(String pkorg, String bfdate, String afdata) {
        BigDecimal totre ;
        BigDecimal totpa;
        List<Map<String, Object>> listMap = new ArrayList<Map<String, Object>>();
        //集团中心流入流出
       Map<String, Object> map = reportMoney.moneyReport( pkorg, bfdate , afdata );
        if(map == null || map.size() == 0){
            map = new HashMap<>();
            map.put("GROUPRE",0);
            map.put("GROUPPA",0);
        }
        totre =new BigDecimal(map.get("GROUPRE").toString());
        totpa =new BigDecimal(map.get("GROUPPA").toString());

      //  listMap.add(map);
        //集团中心，期初期末
      // Map<String, Object> map1 = reportMoney.moneyReportini( pkorg, bfdate , afdata );
      //  listMap.add(map1);

        //本单位中心，流入流出
        Map<String, Object> map2 = reportMoney.moneyReportcr( pkorg, bfdate , afdata );

        if(map2 == null || map2.size() == 0){
            System.out.println("====map2 weikong =====");
            map2 = new HashMap<>();
            map2.put("UNITRE",0);
            map2.put("UNITPA",0);
        }
        System.out.println("====map2 weikong =====" +map2);
        totre =totre.add(new BigDecimal(map2.get("UNITRE").toString()));
        totpa =totpa.add(new BigDecimal(map2.get("UNITPA").toString()));
      //  listMap.add(map2);

        //本单位中心，期初期末
       // Map<String, Object> map3 = reportMoney.moneyReportbini( pkorg, bfdate , afdata );
       // listMap.add(map3);

        //在财务公司存款   流入流出
        Map<String, Object> map4 =   reportMoney.moneyReportfin( pkorg, bfdate , afdata );

        if(map4 == null || map4.size() == 0){
            map4 =  new HashMap<>();
            map4.put("FINANCERE",0);
            map4.put("FINANCEPA",0);
        }

        totre =totre.add(new BigDecimal(map4.get("FINANCERE").toString()));
        totpa =totpa.add(new BigDecimal(map4.get("FINANCEPA").toString()));
       // listMap.add(map4);

        //在其他机构存款   流入流出
        Map<String, Object> map5 =   reportMoney.moneyReportel( pkorg, bfdate , afdata );

        if(map5 == null || map5.size() == 0){
            map5 =  new HashMap<>();
            map5.put("ELSERE",0);
            map5.put("ELSEPA",0);
        }

        totre =totre.add(new BigDecimal(map5.get("ELSERE").toString()));
        totpa =totpa.add(new BigDecimal(map5.get("ELSEPA").toString()));

       // listMap.add(map5);

        Map<String, Object> map6 = new HashMap<>();
        //流入、流出  上周余额、本周余额、
        map6.put("WEEKSYUE",0);
        map6.put("WEEKRE",totre);
        map6.put("WEEKPA",totpa);
        map6.put("WEEKBYUE",0);

        System.out.println("======" +map );

        //应收-上周余额，本周流入、本周流出、本周余额
        map6.put("SHWEEKSYUE",0);
        map6.put("SHWEEKRE",0);
        map6.put("SHWEEKPA",0);
        map6.put("SHWEEKBYUE",0);

        //总计
        /*TOTSYUE		--上周流出
          TOTRE		--本周流入
          TOTPA		--本周流出
          TOTBYUE 		--本周余额*/
        map6.put("TOTSYUE",0);
        map6.put("TOTRE",totre);
        map6.put("TOTPA",totpa);
        map6.put("TOTBYUE",0);

        listMap.add(map6);


        return R.ok().put("data", listMap);
    }


    @Override
    public R moneytbzq(String year) {
            String pid ;

        List<Map<String, Object>> listMap = reportMoney.moneytbzq( );

        List<Map<String,Object>> list = new ArrayList<Map<String,Object>>() ;
        for (Map<String, Object> m : listMap) {
            Map<String, Object> newmap = new HashMap<String, Object>();

        //    for (String k : m.keySet()) {
                String zhongjian = (m.get("WEEK").toString().substring(0,10)).replace('-','/')+"-"+
                        m.get("WEEK").toString().substring(23,34).replace('-','/')+
                        m.get("WEEK").toString().substring(39,46);

                String cc = zhongjian.substring(26,27);


                String zhouqi ;

                switch(cc){
                    case "一":
                        zhouqi= zhongjian.replace('一','1');
                        break;
                    case "二":
                        zhouqi = zhongjian.replace('二','2');
                        break;

                    case "三":
                        zhouqi = zhongjian.replace('三','3');
                        break;
                    case "四":
                        zhouqi = zhongjian.replace('四','4');
                        break;
                    default:
                        zhouqi = "";
                        break;
                }

                /*if(cc.equals("一") ){
                    System.out.println("==11111fffff=" + k + " : " + cc);
                    zhouqi= zhongjian.replace('一','1');
                } else if(cc.equals("二")){
                    System.out.println("==22222fffff=" + k + " : " + cc);
                    zhouqi = zhongjian.replace('二','2');
                } else if (cc.equals("三")) {
                    System.out.println("==ff33333fff=" + k + " : " + cc);
                    zhouqi = zhongjian.replace('三','3');
                }else if(cc.equals("四")){
                    System.out.println("==fff44444ff=" + k + " : " + cc);
                    zhouqi = zhongjian.replace('四','4');
                }else {
                    System.out.println("==ffff55555f=" + k + " : " + cc);
                    zhouqi ="";
                }*/
                newmap.put("PK",m.get("PK"));
                newmap.put("WEEK",zhouqi);

                list.add(newmap);


          //  }
        }
            /*Map<String, Object> pide =   reportMoney.moneytbzqDate( year );
        System.out.println(pide+"=====================");
            pid = pide.get("PK_DEFDOC").toString();
            System.out.println(pid+"=====================");
        List<Map<String, Object>> listMap = reportMoney.moneytbzq( pid);*/

        return R.ok().put("data", list);
    }

}
